<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Module extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }
	 public function searchModule()
	{
		$mcode = $this->input->post('moduleCode');
		$mname = $this->input->post('moduleName');

		$query = $this->module_model->searchModuleDB($mcode, $mname);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}

	}
	public function insert_module(){
		 $mod_dir = $this->input->post('moduleDir');
		 $path_view  = "application/views/".$mod_dir;
		 $path_model = "application/models/".$mod_dir;
		 $path_cont  = "application/controllers/".$mod_dir;
    	if(!is_dir($path_view)) //create the folder if it's not already exists
    	{
      		mkdir($path_view,0755,TRUE);
    	}
    	if(!is_dir($path_model)) //create the folder if it's not already exists
    	{
      		mkdir($path_model,0755,TRUE);
    	}
    	if(!is_dir($path_cont)) //create the folder if it's not already exists
    	{
      		mkdir($path_cont,0755,TRUE);
    	}
    	$ena = $this->input->post('enableBox');
    	$data = array(
    		':p_module_code' 		=> $this->input->post('moduleCode'),
    		':p_module_name' 		=> $this->input->post('moduleName'),
    		':p_dir_path'			=> $mod_dir,
    		':p_tel_num'			=> "",
    		':p_tax_num'			=> "",
    		':p_logo_file_name' 	=> "",
    		':p_enable_flag'		=> ($ena == "ok" ? 'Y' : 'N'),
    		':p_created_by'			=> $this->session->userdata('user_id')
    	);

    	$this->module_model->save_module($data);
	}
	public function insert_module_menu(){

		  $dir;
		  foreach($this->module_model->getModbyID($this->input->post('module-id')) as $mod){
		   $dir = $mod->dir_path;
		  }

		  $path_v1 = $dir.'/screen';
		  $path_v2 = $dir.'/report';

		  if(!is_dir($path_v1)) //create the folder if it's not already exists
		     {
		        mkdir($path_v1,0755,TRUE);
		     }
		  if(!is_dir($path_v2)) //create the folder if it's not already exists
		     {
		        mkdir($path_v2,0755,TRUE);
		     } 
		$data = array(
			':p_module_id'	=> $this->input->post('module-id'),
			':p_menu_name'	=> $this->input->post('menu-name'),
			':p_created_by'	=> $this->session->userdata('user_id')
			);
		$query = $this->module_model->insertModuleMenu($data);
		$this->viewProgramGroup($this->input->post('module-id'));

	}
	public function viewProgramGroup(){
		$id = $this->input->post('module-id');
		$query = $this->module_model->getModuleMenu($id);
		echo json_encode($query);
	}
	public function edit_menu(){
		$menu_id 	= $this->input->post('menu_id_edit');
		$menu_name 	= $this->input->post('menu_name_edit');
		$data = array(
			'p_menu_name'	=> $menu_name,
			'p_updated_by'	=> $this->session->userdata('user_id'),
			'p_menu_id'		=> $menu_id
			);

		$query = $this->module_model->edit_menus($data);
		
		$err ='1'; $msg;
		foreach ($query as $key) {
			$err = $query[0]->error;
			$msg = $query[0]->err_msg;
		}
		$arr = array(
			'err'		=> 'Y',
			'msg' 		=> "Update Successfull",
			'menu_name'	=> $menu_name,
			'menu_id'	=> $menu_id
			);
		if($err == '1'){
			echo json_encode($msg);
		}else{
			echo json_encode($arr);
			}
	}
	public function update_module(){
		$oldPath_c = "application/controllers/".$this->input->post('pastDir');
		$oldPath_m = "application/models/".$this->input->post('pastDir');
		$oldPath_v = "application/views/".$this->input->post('pastDir');
		$this->load->helper("file"); // load the helper

		$path_c = "application/controllers/".$this->input->post('moduleDir');
		$path_m = "application/models/".$this->input->post('moduleDir');
		$path_v = "application/views/".$this->input->post('moduleDir');
		 rename($oldPath_c,$path_c);
		 rename($oldPath_m,$path_m);
		 rename($oldPath_v,$path_v);
    	
    	$ena = $this->input->post('enableBox');
    	$data = array(
    		':p_module_name' 		=> $this->input->post('moduleName'),
    		':p_dir_path'			=> $path_v,
    		':p_enable_flag'		=> ($ena == "ok" ? 'Y' : 'N'),
    		':p_updated_by'			=> $this->session->userdata('user_id'),
    		':p_module_id'			=> $this->input->post('moduleID')
    	);
    	$this->module_model->edit_module($data);

	}

	public function add_form(){
		$this->load->view($this->session->userdata('foradd'));
	}
	public function edit_form($id){
			$module_forms = $this->module_model->getModuleFormMenu($id);
			$result = $this->module_model->getModbyID($id);
			$this->load->view($this->session->userdata('foredit'),array(
					'result'		=>$result,
					'module_data'	=> $module_forms
				));
	}
	public function view_form($id){
			$module_forms = $this->module_model->getModuleFormMenu($id);
			$result = $this->module_model->getModbyID($id);
			$this->load->view($this->session->userdata('forview'),array(
					'result'		=>$result,
					'module_data'	=> $module_forms
				));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */